package com.cloud.drore.eboss.operation.mapper.provider;

import com.cloud.drore.eboos.common.constant.CommonConstant;
import com.cloud.drore.eboos.common.entity.dealer.DistributorProductQueryPo;
import com.cloud.drore.eboss.operation.entity.po.CooperationManagementAddPo;
import com.cloud.drore.eboss.operation.entity.po.CooperationSetPriceForRelationPo;
import com.cloud.drore.eboss.operation.entity.po.DistributorsAllRelationPo;

/**
 * 浙江卓锐科技股份有限公司 版权所有  Copyright 2018<br>
 *
 * @Description: 合作管理关联关系列表 <br>
 * @Project: eboos <br>
 * @CreateDate: Created in 2018/2/9 10:35 <br>
 * @Author: <a href="1451173193@qq.com">杨亮</a>
 */
public class CooperationManagementProvider {

    public String getSetPriceForRelationSql(CooperationSetPriceForRelationPo po) {
        String sql = "update eboos_distributor_product_association set modified_date=now(),sales_price=" + po.getSalesPrice() + " where id=" + po.getRelationId();
        return sql;
    }

    public String getDistributorsAllRelationSql(DistributorsAllRelationPo po) {
        String sql = "select a.main_id,b.created_date,b.product_num,b.product_name,b.product_type product_type,case when a.sales_price is null then a.settlement_price else a.sales_price end sales_price,a.id relation_id  from eboos_distributor_product_association a \n" +
                "left join eboos_product b on a.main_id=b.id \n" +
                "left join eboos_merchant c on c.id=a.distributor_id \n" +
                "where c.merchant_state='"+CommonConstant.MERCHANT_AUDIT_ALREADY+"' and b.status='"+CommonConstant.THE_SHELVES+"' and a.distributor_id=" + po.getDistributorId();
        if (po.getProductName() != null && !po.getProductName().equals("")) {
            sql = sql + " and instr(b.product_name,'" + po.getProductName() + "')>0";
        }
        return sql;
    }

    public String getNewRelationListByParamSql(CooperationManagementAddPo po) {
        //各表字段有异无法统一sql
        StringBuffer sql = new StringBuffer("");
        //门票表

        if (CommonConstant.TYPE_OF_TICKET.equals(po.getProductType())){
            sql.append("select a.id main_id,a.created_date,"+po.getDistributorId()+" distributor_id,a.product_name,a.crowd product_crowd,e.term_of_validity_start_time,e.term_of_validity_end_time, f.settlement_price from eboos_product a\n" +

                    "left join eboos_ticket e on a.product_id=e.id\n" +
                    "left join eboos_ticket_price f on f.ticket_id=e.id\n" +
                    "where a.product_type='"+CommonConstant.TYPE_OF_TICKET+"' " +
                    " and a.status='"+CommonConstant.THE_SHELVES+"' and \n" +
                    "a.id not in (select main_id from eboos_distributor_product_association where distributor_id="+po.getDistributorId()+")");
        }else if(CommonConstant.TYPE_OF_COMBINATION.equals(po.getProductType())){
            sql.append("select a.id main_id,a.created_date, "+po.getDistributorId()+" distributor_id,a.product_name,a.crowd product_crowd,e.term_of_validity_start_time,e.term_of_validity_end_time, f.settlement_price from eboos_product a\n" +

                    "left join eboos_combination_product e on a.product_id=e.id\n" +
                    "left join eboos_combination_product_price f on f.combination_product_id=e.id\n" +
                    "where  a.product_type='"+CommonConstant.TYPE_OF_COMBINATION+"' and a.status='"+CommonConstant.THE_SHELVES+"'  and\n" +
                    "a.id not in (select main_id from eboos_distributor_product_association where distributor_id="+po.getDistributorId()+")");
        }else{
           // throw new BusinessException(BUSINESS_CODE,NOT_SUPPORT_TYPE_ERROR);
        }


        return sql.toString();
    }
    public String findCombinationProductByParamSql(DistributorProductQueryPo po){
        StringBuffer sql=new StringBuffer("");
        sql.append("select b.is_real_name,'组合产品' product_type,b.status,b.id,b.created_date,d.sales_price ,c.consume_name,b.product_crowd,\n" +
                "group_concat(DISTINCT c.consume_poi_name) as poi_names,\n" +
                "group_concat(DISTINCT c.consume_name) as consume_names,\n" +
                "b.product_num,b.product_name,a.consume_id from eboos_product_consume_relation a  \n" +
                "INNER JOIN eboos_distributor_product_association d on d.main_id=a.product_id\n" +
                "INNER JOIN eboos_combination_product b on  a.product_id=b.common_product_id \n" +
                "left join eboos_consume c on a.consume_id=c.id\n" +
                "where b.status='"+CommonConstant.THE_SHELVES+"' and d.distributor_id="+po.getDistributorId()+"");
        if(po.getName()!=null&&!po.getName().equals("")){
            sql.append(" and locate ('"+po.getName()+"',b.product_name)");
        }
        if(po.getPoiId()!=null){
            sql.append(" and c.consume_poi_id="+po.getPoiId());
        }
        sql.append(" group by a.product_id");
        return sql.toString();
    }
    public String findTicketProductByParamSql(DistributorProductQueryPo po){
        StringBuffer sql=new StringBuffer("");
        sql.append("select b.is_real_name,'门票产品' product_type,b.status,b.id,b.created_date,d.sales_price ,c.consume_name,b.crowd product_crowd,\n" +
                "group_concat(DISTINCT c.consume_poi_name) as poi_names,\n" +
                "group_concat(DISTINCT c.consume_name) as consume_names,\n" +
                "b.product_num,b.name product_name,a.consume_id from eboos_product_consume_relation a  \n" +
                "INNER JOIN eboos_distributor_product_association d on d.main_id=a.product_id\n" +
                "INNER JOIN eboos_ticket b on  a.product_id=b.common_product_id \n" +
                "left join eboos_consume c on a.consume_id=c.id " +
                "where b.status='"+CommonConstant.THE_SHELVES+"' and d.distributor_id="+po.getDistributorId()+"");
        if(po.getName()!=null&&!po.getName().equals("")){
            sql.append(" and locate ('"+po.getName()+"',b.name)");
        }
        if(po.getPoiId()!=null){
            sql.append(" and c.consume_poi_id="+po.getPoiId());
        }
        sql.append(" group by a.product_id");
        return sql.toString();
    }
}
